#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<cstring>
#include<cerrno>
#include<sys/wait.h>

const int size=1024;
void Pwrite(int wfd)
{
    std::string message="i am child process";
    while(1)
    {
        write(wfd,message.c_str(),message.size());
        std::cerr<<message<<std::endl;
    }
}

void PRead(int rfd)
{
    char inbuffer[size];
    ssize_t n=read(rfd,inbuffer,sizeof(inbuffer)-1);
    inbuffer[n]='\0';
    int cnt=0;
    while(1)
    {
        std::cout<<"fa get message "<<inbuffer<<",cnt:"<<cnt<<std::endl;
        cnt++;
    }
}



int main()
{
    int pipefd[2];
    int n=pipe(pipefd);
    if(n!=0)
    {
        std::cout<<"errno:"<<errno<<": "<<
        "errsring:"<<strerror(errno)<<std::endl;
    }
    std::cout<<"pipefd[0]:"<<pipefd[0]<<",pipefd[1]"
    <<pipefd[1]<<std::endl;
    pid_t id=fork();

    if(id==0)
    {
        close(pipefd[0]);
        Pwrite(pipefd[1]);
        close(pipefd[1]);
        exit(0);
    }
    
    close(pipefd[1]);
    PRead(pipefd[0]);
    close(pipefd[0]);
    pid_t rid=waitpid(id,nullptr,0);
    return 0;
}